#!/bin/bash


main(){
  containerd_install

}

containerd_install(){
# 先卸载已经安装的dokcer
apt-get remove docker \
  docker-client \
  docker-client-latest \
  docker-common \
  docker-latest \
  docker-latest-logrotate \
  docker-engine docker-ce containerd -y

# 如果服务器没有默认的docker源，首先配置源：
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 安装containerd
apt-get install containerd -y

# 配置containerd的内核
cat << EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

sudo sysctl --system

# 创建containerd的配置文件
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml

sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml
sed -i 's#k8s.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g'  /etc/containerd/config.toml

# 启动containerd
systemctl daemon-reload
systemctl restart containerd
systemctl status containerd
ctr plugin ls

}

# 每个节点都要执行.这个环节需要注意下，/etc/fstab 需要手动修改
kubeadm_kubelet_install(){

# 配置国内源
# 添加apt-key
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg |sudo apt-key add -
# 添加源
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" >  /etc/apt/sources.list.d/kubernetes.list
  sudo apt-get update
sudo apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00  kubectl=1.24.0-00
# 防止更新系统的时候将这些组件更新掉
sudo apt-mark hold kubelet kubeadm kubectl

swapoff -a
vim /etc/fstab 注释自动挂载swapoff,需要手动完成
kubeadm config images pull \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version 1.24.0

# 查看下载镜像
ctr -n k8s.io images ls
}

# Master节点初始化
kubeadm_init_master(){
# ip 地址是当前安装主机的ip地址
kubeadm init --apiserver-advertise-address 192.168.1.10   --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --cri-socket "unix:///var/run/containerd/containerd.sock" --kubernetes-version 1.24.0
# kubeadm reset

# 成功后执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

}

# node节点初始化
kubeadm_init_node(){
system enable kubelet

# 不要有换行
kubeadm join 192.168.1.10:6443 --token pc3735.3jlrcgykzhvycrvi --discovery-token-ca-cert-hash sha256:33fc9fd74ae4f4ae7c23ce0a4d216ebf46a110c67a1f4aebff9fc4d11a0f4640
}

calico(){
kubectl  create -f calico.yaml

}

metrics_server(){
#把/etc/kubernetes/pki/front-proxy-ca.crt复制到所有的Node节点
# 需要同步 master 节点的/etc/kubernetes/pki/front-proxy-ca.crt 文件至 node节点的同名目录下
kubectl  create -f comp.yaml

}

create_dashboard(){
#创建dashboard
kubectl create -f dashboard.yaml -f user.yaml
kubectl  create token admin-user -n kube-system

#eyJhbGciOiJSUzI1NiIsImtpZCI6IkZHTzdFaFVpNFdZcjIteEt2SWpiYUZlenBQQnQ1TXVhRFI1LThCZ280c00ifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNjcwMTcxNTk2LCJpYXQiOjE2NzAxNjc5OTYsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsInNlcnZpY2VhY2NvdW50Ijp7Im5hbWUiOiJhZG1pbi11c2VyIiwidWlkIjoiYTNhMTk0ZTQtZGIwYy00NWVhLWFjYTEtYjVhZGIzZDcyOTE4In19LCJuYmYiOjE2NzAxNjc5OTYsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTphZG1pbi11c2VyIn0.EaCrsVGw3WgnoFFSGwhEfs7K-dtp6vahF6AymuI7Mz3Uz6fkeuIbrmz9ZTSaNF1V0bcueCfrIPqon1wf60m-_kHidh5ISCzuOfO_Bz5xtE2PZ30lxR00wHJcxN9czWT5m3cZFA__HvE6Rq4yP0m1k1HIgYdZNIYqiXdwqJ6whtUk-p6bb7idQRrMge-KS7AAOMlTiCDM6-MM3MoF-TNeR1XEVsZmlgzLMIyAkF7WBcCbZ-bMlmhkT5tdlxxeIYVrceBSedfN-xF91vOn19TEo16Cow-ll_Az6huYLyoF9E7vS5H3lB5pkN0c5nIPJxszPL2ZsxJmVfXrHfDBzOVYkA

}


main
